#!/bin/bash

##
# This is a utility script to track packages right from the terminal
# The whole script is based on htmlparsing so the assumption is that
# the output html from the urls doesn't change :)
# Date: May 06, 2009
##

#tracking urls
UPS_URL="http://wwwapps.ups.com/WebTracking/track?HTMLVersion=5.0&loc=en_US&Requester=UPSHome&trackNums=${2}&track.x=track"
USPS_DATA="strOrigTrackNum=${2}&Go+to+Track+%26+Confirm.x=25&Go+to+Track+%26+Confirm.y=12&Go+to+Track+%26+Confirm=Go"
USPS_URL="http://trkcnfrm1.smi.usps.com/PTSInternetWeb/InterLabelInquiry.do"
FEDEX_URL="http://www.fedex.com/Tracking?tracknumbers=${2}&action=track&language=english"


printUsage () 
{
    echo -e " Packtrack Usage: packtrack -<ups|usps|fedex> <tracking number>"
    return
}

# check if data as fetched and output
printOutput () 
{
    if [ -s "/tmp/packtrack.tmp" ]
    then
        error=
        case $1 in
        -ups)    
            error=$( grep -e 'not a valid tracking number' -e 'UPS could not locate' /tmp/packtrack.tmp )
            ;;
        -usps)
            error=$( grep -e 'There is no record of this item.' /tmp/packtrack.tmp )
            ;;
        -fedex)
            error=
            ;;
        esac

        if [ -n "$error" ]
        then
            echo ' Invalid Tracking Number!!'
        else
            cat /tmp/packtrack.tmp
        fi
    else
        echo "No data fetched. Please check the tracking number and your internet connection."
    fi

    return
}

###### the execution starts here ######
# check arguments
if [ $# -lt 2 ]
then
    printUsage
    exit -1
fi

case $1 in
-ups)
    
    #the main function that fetches and parses the ups data
    curl -s $UPS_URL | \
    tr -d '\r' | \
    tr -d '\n' | \
    sed -e 's/.*Begin Summary List -->//' -e 's/<\!-- End Summary List.*//' -e 's/ \+\|\t\+/ /g' -e 's/>/>\n/g' -e 's/<tr>/#LINE#/g' | \
    sed 's/ *<.*> *//' | \
    tr -d '\n' | \
    sed -e 's/#LINE#/\n/g' -e 's/\&nbsp;//g' | \
    grep -A 1 -m 1 'Status' | \
    cut -d : -f 2,3 > /tmp/packtrack.tmp
    
    printOutput $1
    ;;
-usps)

    #the main function that fetches and parses the usps data
    curl -s -d $USPS_DATA $USPS_URL | \
    tr -d '\n' | tr -d '\r' | \
    sed -e 's/\(\s\)\+/ /g' | \
    sed -e 's/\(\t\)\+//g' | \
    sed -e 's/.*\(<td valign=\"top\" class=\"mainText\">\(\s\)\+Label\/Receipt.*<\/td>\(\s\|\t\)\+<td align=\"right\" valign=\"top\">\(\s\|\t\)\+<form\).*/\1/' | \
    sed -e 's/>/>\n/g' -e 's/<\/span>/#LINE#/g' | \
    sed -e 's/ *<.*//' -e 's/ \+\|\t\+/ /g' | \
    tr -d '\n' | sed -e 's/#LINE#/\n/g' -e 's/ Delivered//' -e 's/-->//' -e 's/ \+/ /' > /tmp/packtrack.tmp

    printOutput $1
    ;;
-fedex)

    #the main function that fetches and parses the usps data
    curl -s $FEDEX_URL | \
    grep detailInfoObject | \
    sed -e 's/\(.*\)\(\"scans\"\:\[{\)/\2/' -e 's/}\],/}\],\n/' | \
    tail -n 1 | sed 's/,\"/,\n\"/g' | \
    grep -e '"status"' -e '"deliveredTo"' -e '"shipDate"' -e '"statusDescription"' -e '"deliveryLocation"' \
    -e '"signedForBy"' -e '"serviceType"' -e "deliveryDateTime" | \
    sed 's/\":\"/ : /' | tr -d '",};' > /tmp/packtrack.tmp

    printOutput $1
    ;;
*)
    printUsage
    ;;
esac
